<!-- eslint-disable vue/no-template-shadow -->
<template>
  <component ref="curNode" :is="curEle">
    <template #col_1_6> </template>
    <template
      v-for="item in slotList"
      #[item.slotCode]="scopeData"
      :key="item.slotCode"
    >
      <slot :name="item.slotCode" v-bind="scopeData"> </slot>
    </template>
  </component>
</template>

<script lang="ts" setup>
import { isFunction } from 'lodash-es'
import { ref, onMounted, computed, nextTick, unref } from 'vue'
import {
  createInsCom,
  BwCustomComFrontTypeEnum,
  type BwCustomComStructModel,
  type BwGetCustomComInfoFun,
  type BwCustomComSlotInfoModel,
  type BwCustomComDesignInfoModel,
  type BwCustomComConventionSlotInfoModel,
} from '@bitwit/base-com-render'
import { getCustomComDesignInfo } from '@/store'
import { getInsBwUtils } from '../../../../utils/setupLogic'

import type { BwVueIns } from '../../../../utils/model'

interface BwCustomComProps {
  vueIns: BwVueIns
  scopeData: Record<string, any>
  nodeInfo: BwCustomComStructModel
  slotList: Array<BwCustomComConventionSlotInfoModel>
  frontType: BwCustomComFrontTypeEnum
}

const emit = defineEmits([])
const props = withDefaults(defineProps<BwCustomComProps>(), {
  slotList: () => [],
})

const curNode = ref()
const curEle = ref<any>(null)
const curSlotList = ref<Array<BwCustomComSlotInfoModel>>([])

const extendBwUtils = computed(() => {
  const utils = getInsBwUtils({ params: {} })
  return utils
})
onMounted(() => {
  const nodeInfo = props.nodeInfo
  const comCode = nodeInfo.comName ?? null
  const appCode = nodeInfo.comAppCode ?? ''

  getCustomComDesignInfo(appCode, comCode).then((res) => {
    if (res) {
      curSlotList.value = res.comSlots ?? []
      createInsCom(
        res as BwCustomComDesignInfoModel,
        props.frontType,
        getCustomComDesignInfo as BwGetCustomComInfoFun,
        unref(extendBwUtils)
      ).then((insCom) => {
        curEle.value = insCom

        nextTick(() => {})
      })
    }
  })
})

defineExpose({
  bwComSlotInfoList() {
    return unref(curSlotList)
  },
  bwSysGetComSlotInfoList() {
    const nodeEle = unref(curNode)
    if (isFunction(nodeEle.bwSysGetComSlotInfoList)) {
      const list = nodeEle.bwSysGetComSlotInfoList()
      return list
    }
  },
})
</script>

<style lang="less" scoped></style>
